import { defineStore } from 'pinia'
import {getProduct,IProduct} from '../api/shop'
export const useProductsStore=defineStore('products',{
    state:()=>{
        return {
            //所有商品列表 (使用as做了类型转换)
            all:[] as IProduct[],
        }
    },
    getters:{

    },
    actions:{
        async loadAllProducts(){
            const result = await getProduct()
            this.all=result
        },
        /**
         * 减少库存
         * @param product 
         */
        decreaseProduct(product:IProduct){
            const result = this.all.find(item=>item.id==product.id)
            if(result){
                result.inventory--
            }
        }
    }
})